import { Tsrv, ifs, ts } from '@/services';
import { N_oSetupPanel } from '@/types';

class T_cmd {
  // 主组件箱
  private compBox: ifs.I_compBox;

  // 主组件ID
  private compID: string;

  private compName: string;

  // 主组件实例
  private self: any;
  // 主组refs

  // 组件配合 在外部实例化
  private TCoordination: ifs.I_compCoordination;

  private toolbarStatusBak: any = null;

  private _config: any;

  private standbyCode: string = '';

  // 主组件created里初始化
  public init(self: any, compBoxInfo: ifs.I_compBoxInfo) {
    this.self = self;
    this.compBox = compBoxInfo.TcompBox;
    this.compID = compBoxInfo.compID;
    this.compName = self.G_compName;
    this._config = Tsrv.utils.clone(self.config, true);
  }

  /**
   * @description: 获取报表图表code
   * @return {*}
   */
  public getReportChartCode() {
    const messageConfig = this._config?.messageConfig;
    // 积木报表URL   reportURL
    let reportURL = Tsrv.utils.clone(this.self.$config.reportURL, true);
    reportURL = reportURL.replace('/jmreport/', '/');
    // 工作台URL  workbenchUrl
    let workbenchUrl = `${reportURL + this.self.chartMode}list?token=${
      this.self.$store.state.User.token
    }`;
    if (this.self.chartMode === 'jmreport') {
      workbenchUrl = `${reportURL + this.self.chartMode}list?token=${
        this.self.$store.state.User.token
      }`;
    } else if (this.self.chartMode === 'jimubi') {
      // workbenchUrl = `${reportURL}drag/list?token=${this.self.$store.state.User.token}`;
      workbenchUrl = `${reportURL}drag/list`;
    }
    // 没关联 有选址
    if (Tsrv.utils.noValue(messageConfig?.custDefListener?.search)) {
      if (!Tsrv.utils.noValue(this._config.reportChartCode)) {
        this.getChartCode(this._config.reportChartCode).then(
          ([b, res, rows, msg]) => {
            this.standbyCode = res.data.rows[0]?.f_code;
            this.self.reportChartCode = res.data.rows[0]?.f_code || '';
            this.self.url = this.getReportUrlByCode(this.self.reportChartCode);
          }
        );
      } else {
        this.self.url = workbenchUrl;
      }
    } else {
      // 有关联
      if (!Tsrv.utils.noValue(this._config.reportChartCode)) {
        // 有选址
        if (Tsrv.utils.noValue(this.standbyCode)) {
          this.getChartCode(this._config.reportChartCode).then(
            ([b, res, rows, msg]) => {
              this.standbyCode = res.data.rows[0]?.f_code;
              if (Tsrv.utils.noValue(this.self.reportChartCode)) {
                this.self.url = this.getReportUrlByCode(this.standbyCode);
              } else {
                this.self.url = this.getReportUrlByCode(
                  this.self.reportChartCode
                );
              }
            }
          );
        } else {
          if (Tsrv.utils.noValue(this.self.reportChartCode)) {
            this.self.url = this.getReportUrlByCode(this.standbyCode);
          } else {
            this.self.url = this.getReportUrlByCode(this.self.reportChartCode);
          }
        }
      } else {
        // 无选址
        if (Tsrv.utils.noValue(this.self.reportChartCode)) {
          this.self.url = workbenchUrl;
        } else {
          this.self.url = this.getReportUrlByCode(this.self.reportChartCode);
        }
      }
    }
  }

  private getReportUrlByCode(code: string): string {
    let reportURL = Tsrv.utils.clone(this.self.$config.reportURL, true);
    reportURL = reportURL.replace('/jmreport/', '/');
    if (this.self.chartMode === 'jmreport') {
      const statusUrl = this.self.chartStatus === 'edit' ? 'index' : 'view';
      reportURL = `${reportURL +
        this.self.chartMode}/${statusUrl}/${code}?token=${
        this.self.$store.state.User.token
      }`;
    } else if (this.self.chartMode === 'jimubi') {
      const statusUrl =
        this.self.chartStatus === 'edit' ? 'bigindex?pageId=' : 'share/view/';
      const linkSymbol = this.self.chartStatus === 'edit' ? '$' : '?';
      reportURL = `${reportURL +
        this.self.chartMode}/${statusUrl}${code}${linkSymbol}token=${
        this.self.$store.state.User.token
      }`;
    }
    return reportURL;
  }

  private getChartCode(val) {
    const gpd = new ts.T_getPartDataArg();
    gpd.setFilter({
      $and: [
        {
          id: val
        }
      ]
    });
    gpd.setFunName(Tsrv.globalVar.tableName.SYS_PRINTSET);
    gpd.setTableName(Tsrv.globalVar.tableName.SYS_PRINTSET);
    gpd.setDbType(Tsrv.globalVar.dbType.MYSQL);
    gpd.setLang(this.self.$i18n.locale);
    gpd.setPage(1, 99999);
    gpd.setRule({} as ifs.I_rule);
    return Tsrv.getData(gpd);
  }

  // 构建组件协作处理类
  public newTcompCoordination(conf: any) {
    // 组件调度
    this.TCoordination = new ts.T_compCoordination(
      conf.name,
      conf.messageConfig,
      {
        setTempToolbarStatusDisabled: (
          p: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
        ) => {
          this.setTempToolbarStatusDisabled(p);
        },
        setTempToolbarStatusUnDisabled: (
          p: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
        ) => {
          this.setTempToolbarStatusUnDisabled(p);
        },
        cleanSubCompData: (
          p: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
        ) => {
          // 通知组件清空
          this.self.cleanSubCompData(p);
        },
        setMask: (p: boolean) => {
          this.self.isMask = p;
        }
      }
    );
  }

  // 处理组件协调
  public receEventCoordination(
    m: N_oSetupPanel.N_comps.I_eventCoordinationMsg
  ) {
    this.TCoordination.receEventCoordination(m)
      .then(([b, msg]) => {})
      .catch(msg => {
        console.error(msg);
        // this.self.$msg(msg);
      });
  }

  private setTempToolbarStatusDisabled(
    p: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
  ) {
    if (Tsrv.utils.noValue(this.toolbarStatusBak)) {
      this.toolbarStatusBak = Tsrv.utils.clone(this.self.toolbarList, true);
      this.self.toolbarList.forEach(el => {
        el.disabled = true;
      });
    }
  }

  private setTempToolbarStatusUnDisabled(
    p: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
  ) {
    if (!Tsrv.utils.noValue(this.toolbarStatusBak)) {
      this.toolbarStatusBak.forEach(e => {
        this.self.toolbarList.forEach(el => {
          if (e.key === el.key) el.disabled = e.disabled;
        });
      });

      this.toolbarStatusBak = null;
    }
  }

  // 处理组件协调
  public sendEventCoordination(
    type: N_oSetupPanel.N_comps.T_eventCoordinationToolbar
  ) {
    const r = this.TCoordination.sendEventCoordination(type, this.self.$bus)
      .then(([b, msg]) => {})
      .catch(msg => {
        console.error(msg);
        // this.self.$msg(msg);
      });
  }
}
interface I_cmd extends T_cmd {}
export { I_cmd, T_cmd };
